package Client.Component;

import Client.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;

public class DrawPanel extends JPanel {
    /*
        该类表示一个绘图板控件
        属性：
            1.ArrayList<LinkedList<Point>> lists
            线段由点集构成，LinkedList存放的就是Point类型的点集
            lists用于存放每一条线段（LinkedList）

            2.boolean pressed
            检测用户时候按下鼠标开始绘画，true为按下，开始绘画
            false为松开，为开始绘画或绘画结束

            3.int drawTime = 0
            表示当前绘画的线段数

            4.Color PanelColor/Color drawColor
            分别表示表示绘图板的背景颜色和画笔颜色

            5.ArrayList<Color> lineColors // 新增：保存每条线段的颜色
         */
    private ArrayList<LinkedList<Point>> lists = new ArrayList<>();
    private boolean pressed = false;
    private int drawTime = 0;
    private Color panelColor = Color.GRAY;
    private Color drawColor = Color.pink;
    private ArrayList<Color> lineColors = new ArrayList<>();


    //构造方法
    public DrawPanel(){
        MyMouseListener l = new MyMouseListener();

        //如果为绘制方，应该添加鼠标监听提供绘图，否则不添加鼠标监听不允许绘画
        if(GameFrame.status.equals("DRAW")){
            this.addMouseMotionListener(l);
            this.addMouseListener(l);
        }

    }

    //绘图板的绘制
    @Override
    protected void paintComponent(Graphics g) {
        //绘制一下绘画板
        super.paintComponent(g);
        g.setColor(panelColor);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(drawColor);

        //绘制方绘图
        if(!lists.isEmpty()){
            for (int i = 0; i < lists.size(); i++) {
                LinkedList<Point> list = lists.get(i);
                g.setColor(lineColors.get(i));
                Point p1 = list.get(0);
                for (int j = 1; j < list.size(); j++) {
                    Point p2 = list.get(j);
                    g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
                    p1 = p2;
                }
            }
        }
    }

    //内部类，可以实现部分鼠标监听接口
    private class MyMouseListener extends MouseAdapter{
        @Override
        public void mouseDragged(MouseEvent e) {
            if(pressed) {
//                System.out.println(e.getPoint());
                lists.get(drawTime).add(e.getPoint());
                repaint();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
//            System.out.println("true");
            lists.add(new LinkedList<Point>());
            lists.get(drawTime).add(e.getPoint());
            lineColors.add(drawColor);
            pressed = true;
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            lists.get(drawTime).add(e.getPoint());
            drawTime++;
            repaint();
            //每绘制一次向服务端发送一次绘制请求
            ArrayList<Object> command = new ArrayList<>();
            command.add("绘制");
            command.add(drawTime);
            command.add(new ArrayList<>(lists));
            command.add(new ArrayList<>(lineColors));
            try {
                sendCommand(command);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public void sendCommand(ArrayList<Object> command) throws IOException {
        System.out.println("准备发送数据");
        if(!Util.socket.isClosed()){
            Util.oos.writeObject(command);
        }
        else
            JOptionPane.showMessageDialog(this, "服务器已关闭");
    }

    /*******************一些get和set方法*******************/


    public void setLists(ArrayList<LinkedList<Point>> lists) {
        this.lists = lists;
    }

    public boolean isPressed() {
        return pressed;
    }

    public void setDrawTime(int drawTime){
        this.drawTime = drawTime;
    }
    public void setPanelColor(Color color){
        panelColor = color;
    }

    public void setDrawColor(Color color){
        drawColor = color;
    }

    public ArrayList<LinkedList<Point>> getLists() {
        return lists;
    }

    public int getDrawTime(){
        return drawTime;
    }

    public Color getPanelColor(){
        return panelColor;
    }

    public Color getDrawColor(){
        return drawColor;
    }

    public ArrayList<Color> getLineColors() {
        return lineColors;
    }

    public void setLineColors(ArrayList<Color> lineColors) {
        this.lineColors = lineColors;
    }

}
